package com.dumengwei.senior1.week2.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dumengwei.senior1.week2.entity.Dantou;
import com.dumengwei.senior1.week2.service.DantouService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class MyController {
	
		@Autowired
		private DantouService dantouService;
		
		
		@RequestMapping("list")
		public String list(@RequestParam(defaultValue="1")Integer pageNum,Dantou dantou,Model model){
			PageHelper.startPage(pageNum, 3);
			List<Dantou>list=dantouService.list(dantou);
			PageInfo<Dantou> pageInfo = new PageInfo<>(list);
			model.addAttribute("pg", pageInfo);
			model.addAttribute("list", list);
			model.addAttribute("dantou", dantou);
			return "list";
		}
		
		
		@RequestMapping("delete")
		@ResponseBody
		public Object delete(Integer contractNo){
			return dantouService.delete(contractNo);
		}
		
		@RequestMapping("toUpdate")
		public String toUpdate(Integer contractNo,Model model){
			Dantou dantou=dantouService.finfById(contractNo);
			model.addAttribute("d", dantou);
			return "update";
		}
		
		@RequestMapping("update")
		@ResponseBody
		public Object update(Integer contractNo){
			return dantouService.update(contractNo);
		}
		
		
		@RequestMapping("toAdd")
		public String toAdd(){
			return "add";
		}
		
		@RequestMapping("add")
		public String add(Dantou dantou){
			 dantouService.add(dantou);
			 return "redirect:list";
		}
}
